package com.taotao.manage.controller.api;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import com.taotao.common.bean.ItemCatResult;
import com.taotao.manage.service.ItemCatService;

@Controller
@RequestMapping("api/item/cat")
public class ApiItemCatController {
	@Autowired
	private ItemCatService itemCatService;
	//private static final ObjectMapper mapper = new ObjectMapper();        
	@RequestMapping(method=RequestMethod.GET)
public ResponseEntity<ItemCatResult> queryItemCat(){
		try {
			//调用service查询结果
			ItemCatResult catResult = this.itemCatService.queryAllToTree();
			if (catResult ==null) {
				//404结果为空
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			//204返回成功
			return ResponseEntity.ok(catResult);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//500报异常
	return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	
}
	 /**
     * 查询全部商品类目信息，并封装为tree结构
     * @return
     */
//    @RequestMapping(method = RequestMethod.GET)
//    public ResponseEntity<String> queryItemCat(@RequestParam(value="callback", required = false)String callback) {
//        try {
//            // 调用service获取结果
//            ItemCatResult result = this.itemCatService.queryAllToTree();
//            
//            // 将结果转为JSON
//            String jsonResult = mapper.writeValueAsString(result);
//            // 判断是否是跨域请求,如果是，则添加跨域回调函数
//            if(StringUtils.isNotEmpty(callback)){
//                return ResponseEntity.ok(callback + "("+jsonResult+")");
//            }
//            // 如果不是，直接返回结果和成功状态码
//            return ResponseEntity.ok(jsonResult);
//        } catch (Exception e) {
//            e.printStackTrace();
//            // 返回异常状态码
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
//        }
//    }
}

